<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8"/>
    <title>商品编辑</title>
    <script type="text/javascript" src="/static/js/form-check.js"></script>
    <script type="text/javascript">
        let editForm, commBarCode, jhPrice, commBarCodeWarn, jhPriceWarn;
        window.onload = function () {
            const mySelect = document.getElementById("mySelect");
            const opts = mySelect.options;
            for (let i = 0; i < opts.length; i++) {
                if (opts[i].value == ${result.state!'-1'}) {
                    opts[i].selected = true;
                }
            }
            initViews();
            bindListeners();
        };

        //加载试图元素
        function initViews() {
            editForm = document.getElementById("editForm");
            commBarCode = document.getElementById("commBarCode");
            jhPrice = document.getElementById("jhPrice");
            commBarCodeWarn = document.getElementById("commBarCodeWarn");
            jhPriceWarn = document.getElementById("jhPriceWarn");
        }

        //绑定事件监听
        function bindListeners() {
            editForm.onsubmit = submitCheck;
        }

        function submitCheck() {
            const arr = [];
            const ids = document.getElementsByName("channelIds");
            for (let i = 0; i < ids.length; i++) {
                if (ids[i].checked)
                    arr.push(1);
            }
            if (arr.length === 0) {
                alert("请至少选择一个销售渠道");
                return false;
            }
            return checkNumber(commBarCode, commBarCodeWarn) && checkDecimalMtZero(jhPrice, jhPriceWarn);
        }

    </script>
</head>
<body>
<form action="/comm/updateComm.do" method="post" id="editForm">
    <input type="hidden" value="${result.id}" name="id"/>
    <table cellspacing="0" cellpadding="5" border="0">
        <tr>
            <td>商品名称</td>
            <td>${result.commName}</td>
        </tr>

        <tr>
            <td>品牌</td>
            <td>${result.commBrandBean.brandName}</td>
        </tr>

        <tr>
            <td>商品类型</td>
            <td>${result.commTypeBean.typeName}</td>
        </tr>

        <tr>
            <td>商品编码</td>
            <td>${result.commCode}</td>
        </tr>

        <tr>
            <td>商品状态</td>
            <td>
                <select name="state" id="mySelect">
                    <option value="0">未上架</option>
                    <option value="1">上架</option>
                    <option value="2">促销</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>商品条形码</td>
            <td><input type="text" id="commBarCode" name="commBarCode" value="${result.commBarCode}"/><span
                        id="commBarCodeWarn"></span></td>
        </tr>

        <tr>
            <td>进货价</td>
            <td><input type="text" id="jhPrice" name="jhPrice" value="${result.jhPrice}"/><span id="jhPriceWarn"></span>
            </td>
        </tr>

        <tr>
            <td>销售渠道</td>
            <td>
                <table cellpadding="2" cellspacing="0" border="1">
                    <tr aria-colspan="4" style="text-align: right">
                        <input type="button" value="新增渠道"
                               onclick="location.href='/comm/goToAddChannel?successPage=/comm/findCommById.do?id=${result.id}'"/>
                    </tr>
                    <tr>
                        <th>渠道</th>
                        <th>售价</th>
                        <th>选择</th>
                    </tr>
                    <#list channels as data>
                        <tr>
                            <td>${data.channelName}</td>
                            <td>${result.cbPrice + (result.cbPrice * data.price)}</td>
                            <td>
                                <input type="checkbox" value="${data.id}" name="channelIds"
                                        <#list result.channelList as list>
                                    <#if list.id == data.id>
                                        checked
                                    </#if>
                                        </#list>/>
                            </td>
                        </tr>
                    </#list>
                </table>
            </td>
        </tr>

        <tr>
            <td>商品信息</td>
            <td>
                <table cellspacing="0" cellpadding="3" border="0">
                    <tr>
                        <td>容量：</td>
                        <td>${result.capacity!'N/A'}</td>
                        <td>重量：</td>
                        <td>${result.weight!'N/A'}</td>
                    </tr>
                    <tr>
                        <td>产地：</td>
                        <td>${result.origin!'N/A'}</td>
                        <td>包装单位：</td>
                        <td>${result.packingUnit!'N/A'}</td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center">
                <input type="submit" value="保存"/>
                &nbsp;&nbsp;&nbsp;
                <input type="button" value="返回" onclick="window.history.back()"/>
                &nbsp;&nbsp;&nbsp;
            </td>
        </tr>
    </table>

</form>
</body>
</html>